home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 July: Mac OS SDK / Dev.CD Jul 00 SDK2.toast / Development Kits / Hardware / Mac OS USB DDK / Mac OS USB DDK 1.4.1 / Examples / USBKeypad / USBKeypadKeyIn.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-25  |  24.6 KB  |  997 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        USBKeypadKeyIn.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History:
  11.  
  12.         Jan 00    gcummings    Add Time Manager task to support Force Quit key sequence
  13.                             Add MyPostEvent glue code to accomodate the ForceQuit key patch
  14.                                 which is expecting a long EventKind parameter instead of
  15.                                 a word size parameter.
  16. */
  17.  
  18.  
  19. /*
  20.  USB Keyboard Translation to Macintosh 
  21.  */
  22.  
  23. #include <Types.h>
  24. #include <Events.h>
  25. #include <Resources.h>
  26. #include <LowMem.h>
  27. #include <USB.h>
  28. #include <Timer.h>
  29. #include "USBKeypad.h"
  30.  
  31. extern    usbKeyPadPBStruct myParamBlock;
  32. extern    usbKeyPadPBStruct shimParamBlock;
  33.  
  34.  
  35. #define DOWN            0
  36. #define UP                1
  37.  
  38. #define TRUE            1
  39. #define FALSE            0
  40.  
  41. #define FakeADBAddr        16
  42. #define FakeKBDType        2        // this should be the same as the Apple extended keyboard for now
  43.  
  44. #define kRawEscapeKey    0x35
  45.  
  46. typedef struct {
  47.     TMTask         theTask;
  48.     EventKind     eventNum;
  49.     UInt32         eventMsg;
  50. } TMTaskData;
  51.  
  52. static UInt32    myKeyMAP[4];
  53. static UInt32    keyTransState;
  54. static Handle    handleKCHR;
  55. static UInt8*    KCHRptr;
  56. static TMTaskData gTMTaskData;
  57.  
  58. typedef KeyMap * KeyMapPtr;
  59.  
  60. /* prototypes */
  61. Boolean KeyInArray(UInt8 key, UInt8 *array, UInt16 len);
  62. Boolean SetBit(UInt8 *bitmapArray, UInt16 index, Boolean value); 
  63. void  PostADBKeyToMac(UInt16 virtualKeycode, UInt8 state);
  64. pascal OSErr MyPostEvent(EventKind eventNum, UInt32 eventMsg);
  65. void TimerCompletion(TMTaskData* taskData);
  66.  
  67. /* when we move to master interfaces we can get this stuff from LowMemPriv.h */
  68. /* be sure to turn on DIRECT_LOWMEM_ACCESSORS */
  69. #define LMSetKbdVars(value) ((*(short *)0x0216) = (value))
  70. #define LMSetKeyLast(value) ((*(short *)0x0184) = (value))
  71. #define LMSetKeyTime(value) ((*(long *)0x0186) = (value))
  72. #define LMSetKeyRepTime(value) ((*(long *)0x018A) = (value))
  73. #define LMSetKeyMap(KeyMapValue)    BlockMove((Ptr)(KeyMapValue), (Ptr)0x0174, sizeof(KeyMap))
  74.  
  75. // index represents USB keyboard usage value, content is Mac virtual keycode
  76. static UInt8    USBKMAPNumLockDown[256] = {  
  77.     0xFF,     /* 00 no event */        
  78.     0xFF,    /* 01 ErrorRollOver */    
  79.     0xFF,    /* 02 POSTFail */    
  80.     0xFF,    /* 03 ErrorUndefined */    
  81.     0x00,    /* 04 A */
  82.     0x0B,    /* 05 B */
  83.     0x08,    /* 06 C */
  84.     0x02,    /* 07 D */
  85.     0x0E,    /* 08 E */
  86.     0x03,    /* 09 F */
  87.     0x05,    /* 0A G */
  88.     0x04,    /* 0B H */
  89.     0x22,    /* 0C I */
  90.     0x26,    /* 0D J */
  91.     0x28,    /* 0E K */
  92.     0x25,    /* 0F L */
  93.  
  94.     0x2E,     /* 10 M */        
  95.     0x2D,    /* 11 N */    
  96.     0x1F,    /* 12 O */    
  97.     0x23,    /* 13 P */    
  98.     0x0C,    /* 14 Q */
  99.     0x0F,    /* 15 R */
  100.     0x01,    /* 16 S */
  101.     0x11,    /* 17 T */
  102.     0x20,    /* 18 U */
  103.     0x09,    /* 19 V */
  104.     0x0D,    /* 1A W */
  105.     0x07,    /* 1B X */
  106.     0x10,    /* 1C Y */
  107.     0x06,    /* 1D Z */
  108.     0x12,    /* 1E 1/! */
  109.     0x13,    /* 1F 2/@ */
  110.  
  111.     0x14,     /* 20 3 # */        
  112.     0x15,    /* 21 4 $ */    
  113.     0x17,    /* 22 5 % */    
  114.     0x16,    /* 23 6 ^ */    
  115.     0x1A,    /* 24 7 & */
  116.     0x1C,    /* 25 8 * */
  117.     0x19,    /* 26 9 ( */
  118.     0x1D,    /* 27 0 ) */
  119.     0x24,    /* 28 Return (Enter) */
  120.     0x35,    /* 29 ESC */
  121.     0x33,    /* 2A Delete (Backspace) */
  122.     0x30,    /* 2B Tab */
  123.     0x31,    /* 2C Spacebar */
  124.     0x1B,    /* 2D - _ */
  125.     0x18,    /* 2E = + */
  126.     0x21,    /* 2F [ { */
  127.  
  128.     0x1E,     /* 30 ] } */        
  129.     0x2A,    /* 31 \ | */    
  130.     0xFF,    /* 32 Non-US # and ~ (what?!!!) */    
  131.     0x29,    /* 33 ; : */    
  132.     0x27,    /* 34 ' " */
  133.     0x32,    /* 35 ` ~ */
  134.     0x2B,    /* 36 , < */
  135.     0x2F,    /* 37 . > */
  136.     0x2C,    /* 38 / ? */
  137.     0x39,    /* 39 Caps Lock */
  138.     0x7A,    /* 3A F1 */
  139.     0x78,    /* 3B F2 */
  140.     0x63,    /* 3C F3 */
  141.     0x76,    /* 3D F4 */
  142.     0x60,    /* 3E F5 */
  143.     0x61,    /* 3F F6 */
  144.  
  145.     0x62,     /* 40 F7 */        
  146.     0x64,    /* 41 F8 */    
  147.     0x65,    /* 42 F9 */    
  148.     0x6D,    /* 43 F10 */    
  149.     0x67,    /* 44 F11 */
  150.     0x6F,    /* 45 F12 */
  151.     0x69,    /* 46 F13/PrintScreen */
  152.     0x6B,    /* 47 F14/ScrollLock */
  153.     0x71,    /* 48 F15/Pause */                
  154.     0x72,    /* 49 Insert */
  155.     0x73,    /* 4A Home */
  156.     0x74,    /* 4B PageUp */
  157.     0x75,    /* 4C Delete Forward */
  158.     0x77,    /* 4D End */
  159.     0x79,    /* 4E PageDown */
  160.     0x7C,    /* 4F RightArrow */
  161.  
  162.     0x7B,     /* 50 LeftArrow */        
  163.     0x7D,    /* 51 DownArrow */    
  164.     0x7E,    /* 52 UpArrow */    
  165.     0x47,    /* 53 NumLock/Clear */    
  166.     0x4B,    /* 54 Keypad / */
  167.     0x43,    /* 55 Keypad * */
  168.     0x4E,    /* 56 Keypad - */
  169.     0x45,    /* 57 Keypad + */
  170.     0x4C,    /* 58 Keypad Enter */
  171.     0x53,    /* 59 Keypad 1 */
  172.     0x54,    /* 5A Keypad 2 */
  173.     0x55,    /* 5B Keypad 3 */
  174.     0x56,    /* 5C Keypad 4 */
  175.     0x57,    /* 5D Keypad 5 */
  176.     0x58,    /* 5E Keypad 6 */
  177.     0x59,    /* 5F Keypad 7 */
  178.  
  179.     0x5B,     /* 60 Keypad 8 */        
  180.     0x5C,    /* 61 Keypad 9 */    
  181.     0x52,    /* 62 Keypad 0 */    
  182.     0x41,    /* 63 Keypad . */    
  183.     0xFF,    /* 64 Non-US \ and  | (what ??!!) */
  184.     0x6E,    /* 65 ApplicationKey (not on a mac!)*/
  185.     0x7F,    /* 66 PowerKey  */
  186.     0x51,    /* 67 Keypad = */
  187.     0x69,    /* 68 F13 */
  188.     0x6B,    /* 69 F14 */
  189.     0x71,    /* 6A F15 */
  190.     0xFF,    /* 6B F16 */
  191.     0xFF,    /* 6C F17 */
  192.     0xFF,    /* 6D F18 */
  193.     0xFF,    /* 6E F19 */
  194.     0xFF,    /* 6F F20 */
  195.  
  196.     0x5B,     /* 70 F21 */        
  197.     0x5C,    /* 71 F22 */    
  198.     0x52,    /* 72 F23 */    
  199.     0x41,    /* 73 F24 */    
  200.     0xFF,    /* 74 Execute */
  201.     0xFF,    /* 75 Help */
  202.     0x7F,    /* 76 Menu */
  203.     0x4C,    /* 77 Select */
  204.     0x69,    /* 78 Stop */
  205.     0x6B,    /* 79 Again */
  206.     0x71,    /* 7A Undo */
  207.     0xFF,    /* 7B Cut */
  208.     0xFF,    /* 7C Copy */
  209.     0xFF,    /* 7D Paste */
  210.     0xFF,    /* 7E Find */
  211.     0xFF,    /* 7F Mute */
  212.     
  213.     0xFF,     /* 80 no event */        
  214.     0xFF,    /* 81 no event */    
  215.     0xFF,    /* 82 no event */    
  216.     0xFF,    /* 83 no event */    
  217.     0xFF,    /* 84 no event */
  218.     0xFF,    /* 85 no event */
  219.     0xFF,    /* 86 no event */
  220.     0xFF,    /* 87 no event */
  221.     0xFF,    /* 88 no event */
  222.     0xFF,    /* 89 no event */
  223.     0xFF,    /* 8A no event */
  224.     0xFF,    /* 8B no event */
  225.     0xFF,    /* 8C no event */
  226.     0xFF,    /* 8D no event */
  227.     0xFF,    /* 8E no event */
  228.     0xFF,    /* 8F no event */
  229.  
  230.     0xFF,     /* 90 no event */        
  231.     0xFF,    /* 91 no event */    
  232.     0xFF,    /* 92 no event */    
  233.     0xFF,    /* 93 no event */    
  234.     0xFF,    /* 94 no event */
  235.     0xFF,    /* 95 no event */
  236.     0xFF,    /* 96 no event */
  237.     0xFF,    /* 97 no event */
  238.     0xFF,    /* 98 no event */
  239.     0xFF,    /* 99 no event */
  240.     0xFF,    /* 9A no event */
  241.     0xFF,    /* 9B no event */
  242.     0xFF,    /* 9C no event */
  243.     0xFF,    /* 9D no event */
  244.     0xFF,    /* 9E no event */
  245.     0xFF,    /* 9F no event */
  246.  
  247.     0xFF,     /* A0 no event */        
  248.     0xFF,    /* A1 no event */    
  249.     0xFF,    /* A2 no event */    
  250.     0xFF,    /* A3 no event */    
  251.     0xFF,    /* A4 no event */
  252.     0xFF,    /* A5 no event */
  253.     0xFF,    /* A6 no event */
  254.     0xFF,    /* A7 no event */
  255.     0xFF,    /* A8 no event */
  256.     0xFF,    /* A9 no event */
  257.     0xFF,    /* AA no event */
  258.     0xFF,    /* AB no event */
  259.     0xFF,    /* AC no event */
  260.     0xFF,    /* AD no event */
  261.     0xFF,    /* AE no event */
  262.     0xFF,    /* AF no event */
  263.  
  264.     0xFF,     /* B0 no event */        
  265.     0xFF,    /* B1 no event */    
  266.     0xFF,    /* B2 no event */    
  267.     0xFF,    /* B3 no event */    
  268.     0xFF,    /* B4 no event */
  269.     0xFF,    /* B5 no event */
  270.     0xFF,    /* B6 no event */
  271.     0xFF,    /* B7 no event */
  272.     0xFF,    /* B8 no event */
  273.     0xFF,    /* B9 no event */
  274.     0xFF,    /* BA no event */
  275.     0xFF,    /* BB no event */
  276.     0xFF,    /* BC no event */
  277.     0xFF,    /* BD no event */
  278.     0xFF,    /* BE no event */
  279.     0xFF,    /* BF no event */
  280.  
  281.     0xFF,     /* C0 no event */        
  282.     0xFF,    /* C1 no event */    
  283.     0xFF,    /* C2 no event */    
  284.     0xFF,    /* C3 no event */    
  285.     0xFF,    /* C4 no event */
  286.     0xFF,    /* C5 no event */
  287.     0xFF,    /* C6 no event */
  288.     0xFF,    /* C7 no event */
  289.     0xFF,    /* C8 no event */
  290.     0xFF,    /* C9 no event */
  291.     0xFF,    /* CA no event */
  292.     0xFF,    /* CB no event */
  293.     0xFF,    /* CC no event */
  294.     0xFF,    /* CD no event */
  295.     0xFF,    /* CE no event */
  296.     0xFF,    /* CF no event */
  297.  
  298.     0xFF,     /* D0 no event */        
  299.     0xFF,    /* D1 no event */    
  300.     0xFF,    /* D2 no event */    
  301.     0xFF,    /* D3 no event */    
  302.     0xFF,    /* D4 no event */
  303.     0xFF,    /* D5 no event */
  304.     0xFF,    /* D6 no event */
  305.     0xFF,    /* D7 no event */
  306.     0xFF,    /* D8 no event */
  307.     0xFF,    /* D9 no event */
  308.     0xFF,    /* DA no event */
  309.     0xFF,    /* DB no event */
  310.     0xFF,    /* DC no event */
  311.     0xFF,    /* DD no event */
  312.     0xFF,    /* DE no event */
  313.     0xFF,    /* DF no event */
  314.  
  315.     0x3B,     /* E0 left control key */        
  316.     0x38,    /* E1 left shift key key */    
  317.     0x3A,    /* E2 left alt/option key */    
  318.     0x37,    /* E3 left GUI (windows/cmd) key */    
  319.     
  320.     0x3E,    /* E4 right control key */
  321.     0x3C,    /* E5 right shift key key */
  322.     0x3D,    /* E6 right alt/option key */
  323.     0x37,    /* E7 right GUI (windows/cmd) key */
  324.     0xFF,    /* E8 no event */
  325.     0xFF,    /* E9 no event */
  326.     0xFF,    /* EA no event */
  327.     0xFF,    /* EB no event */
  328.     0xFF,    /* EC no event */
  329.     0xFF,    /* ED no event */
  330.     0xFF,    /* EE no event */
  331.     0xFF,    /* EF no event */
  332.     
  333.     0xFF,     /* F0 no event */        
  334.     0xFF,    /* F1 no event */    
  335.     0xFF,    /* F2 no event */    
  336.     0xFF,    /* F3 no event */    
  337.     0xFF,    /* F4 no event */
  338.     0xFF,    /* F5 no event */
  339.     0xFF,    /* F6 no event */
  340.     0xFF,    /* F7 no event */
  341.     0xFF,    /* F8 no event */
  342.     0xFF,    /* F9 no event */
  343.     0xFF,    /* FA no event */
  344.     0xFF,    /* FB no event */
  345.     0xFF,    /* FC no event */
  346.     0xFF,    /* FD no event */
  347.     0xFF,    /* FE no event */
  348.     0xFF,    /* FF no event */
  349. };
  350.         
  351. // index represents USB keyboard usage value, content is Mac virtual keycode
  352. static UInt8    USBKMAPNumLockUp[256] = {  
  353.     0xFF,     /* 00 no event */        
  354.     0xFF,    /* 01 ErrorRollOver */    
  355.     0xFF,    /* 02 POSTFail */    
  356.     0xFF,    /* 03 ErrorUndefined */    
  357.     0x00,    /* 04 A */
  358.     0x0B,    /* 05 B */
  359.     0x08,    /* 06 C */
  360.     0x02,    /* 07 D */
  361.     0x0E,    /* 08 E */
  362.     0x03,    /* 09 F */
  363.     0x05,    /* 0A G */
  364.     0x04,    /* 0B H */
  365.     0x22,    /* 0C I */
  366.     0x26,    /* 0D J */
  367.     0x28,    /* 0E K */
  368.     0x25,    /* 0F L */
  369.  
  370.     0x2E,     /* 10 M */        
  371.     0x2D,    /* 11 N */    
  372.     0x1F,    /* 12 O */    
  373.     0x23,    /* 13 P */    
  374.     0x0C,    /* 14 Q */
  375.     0x0F,    /* 15 R */
  376.     0x01,    /* 16 S */
  377.     0x11,    /* 17 T */
  378.     0x20,    /* 18 U */
  379.     0x09,    /* 19 V */
  380.     0x0D,    /* 1A W */
  381.     0x07,    /* 1B X */
  382.     0x10,    /* 1C Y */
  383.     0x06,    /* 1D Z */
  384.     0x12,    /* 1E 1/! */
  385.     0x13,    /* 1F 2/@ */
  386.  
  387.     0x14,     /* 20 3 # */        
  388.     0x15,    /* 21 4 $ */    
  389.     0x17,    /* 22 5 % */    
  390.     0x16,    /* 23 6 ^ */    
  391.     0x1A,    /* 24 7 & */
  392.     0x1C,    /* 25 8 * */
  393.     0x19,    /* 26 9 ( */
  394.     0x1D,    /* 27 0 ) */
  395.     0x24,    /* 28 Return (Enter) */
  396.     0x35,    /* 29 ESC */
  397.     0x33,    /* 2A Delete (Backspace) */
  398.     0x30,    /* 2B Tab */
  399.     0x31,    /* 2C Spacebar */
  400.     0x1B,    /* 2D - _ */
  401.     0x18,    /* 2E = + */
  402.     0x21,    /* 2F [ { */
  403.  
  404.     0x1E,     /* 30 ] } */        
  405.     0x2A,    /* 31 \ | */    
  406.     0xFF,    /* 32 Non-US # and ~ (what?!!!) */    
  407.     0x29,    /* 33 ; : */    
  408.     0x27,    /* 34 ' " */
  409.     0x32,    /* 35 ` ~ */
  410.     0x2B,    /* 36 , < */
  411.     0x2F,    /* 37 . > */
  412.     0x2C,    /* 38 / ? */
  413.     0x39,    /* 39 Caps Lock */
  414.     0x7A,    /* 3A F1 */
  415.     0x78,    /* 3B F2 */
  416.     0x63,    /* 3C F3 */
  417.     0x76,    /* 3D F4 */
  418.     0x60,    /* 3E F5 */
  419.     0x61,    /* 3F F6 */
  420.  
  421.     0x62,     /* 40 F7 */        
  422.     0x64,    /* 41 F8 */    
  423.     0x65,    /* 42 F9 */    
  424.     0x6D,    /* 43 F10 */    
  425.     0x67,    /* 44 F11 */
  426.     0x6F,    /* 45 F12 */
  427.     0x69,    /* 46 F13/PrintScreen */
  428.     0x6B,    /* 47 F14/ScrollLock */
  429.     0x71,    /* 48 F15/Pause */                
  430.     0x72,    /* 49 Insert */
  431.     0x73,    /* 4A Home */
  432.     0x74,    /* 4B PageUp */
  433.     0x75,    /* 4C Delete Forward */
  434.     0x77,    /* 4D End */
  435.     0x79,    /* 4E PageDown */
  436.     0x7C,    /* 4F RightArrow */
  437.  
  438.     0x7B,     /* 50 LeftArrow */        
  439.     0x7D,    /* 51 DownArrow */    
  440.     0x7E,    /* 52 UpArrow */    
  441.     0x47,    /* 53 NumLock/Clear */    
  442.     0x4B,    /* 54 Keypad / */
  443.     0x43,    /* 55 Keypad * */
  444.     0x4E,    /* 56 Keypad - */
  445.     0x45,    /* 57 Keypad + */
  446.     0x4C,    /* 58 Keypad Enter */
  447.     0x77,    /* 59 Keypad 1 = End on keyapd*/
  448.     0x7D,    /* 5A Keypad 2 = Down Arrow on keypad*/
  449.     0x79,    /* 5B Keypad 3 = Page Down on keypad*/
  450.     0x7B,    /* 5C Keypad 4 = Left Arrow on keypad*/
  451.     0x57,    /* 5D Keypad 5 */
  452.     0x7C,    /* 5E Keypad 6 = Right Arrow on keypad*/
  453.     0x73,    /* 5F Keypad 7 = HOME on keypad*/
  454.  
  455.     0x7E,     /* 60 Keypad 8 = Up Arrow on keypad*/        
  456.     0x74,    /* 61 Keypad 9 = Page Up on keypad*/    
  457.     0x72,    /* 62 Keypad 0 = Insert on keypad*/    
  458.     0x75,    /* 63 Keypad . = Delete (forward) on keypad*/    
  459.     0xFF,    /* 64 Non-US \ and  | (what ??!!) */
  460.     0x6E,    /* 65 ApplicationKey (not on a mac!)*/
  461.     0x7F,    /* 66 PowerKey  */
  462.     0x51,    /* 67 Keypad = */
  463.     0x69,    /* 68 F13 */
  464.     0x6B,    /* 69 F14 */
  465.     0x71,    /* 6A F15 */
  466.     0xFF,    /* 6B F16 */
  467.     0xFF,    /* 6C F17 */
  468.     0xFF,    /* 6D F18 */
  469.     0xFF,    /* 6E F19 */
  470.     0xFF,    /* 6F F20 */
  471.  
  472.     0x5B,     /* 70 F21 */        
  473.     0x5C,    /* 71 F22 */    
  474.     0x52,    /* 72 F23 */    
  475.     0x41,    /* 73 F24 */    
  476.     0xFF,    /* 74 Execute */
  477.     0xFF,    /* 75 Help */
  478.     0x7F,    /* 76 Menu */
  479.     0x4C,    /* 77 Select */
  480.     0x69,    /* 78 Stop */
  481.     0x6B,    /* 79 Again */
  482.     0x71,    /* 7A Undo */
  483.     0xFF,    /* 7B Cut */
  484.     0xFF,    /* 7C Copy */
  485.     0xFF,    /* 7D Paste */
  486.     0xFF,    /* 7E Find */
  487.     0xFF,    /* 7F Mute */
  488.     
  489.     0xFF,     /* 80 no event */        
  490.     0xFF,    /* 81 no event */    
  491.     0xFF,    /* 82 no event */    
  492.     0xFF,    /* 83 no event */    
  493.     0xFF,    /* 84 no event */
  494.     0xFF,    /* 85 no event */
  495.     0xFF,    /* 86 no event */
  496.     0xFF,    /* 87 no event */
  497.     0xFF,    /* 88 no event */
  498.     0xFF,    /* 89 no event */
  499.     0xFF,    /* 8A no event */
  500.     0xFF,    /* 8B no event */
  501.     0xFF,    /* 8C no event */
  502.     0xFF,    /* 8D no event */
  503.     0xFF,    /* 8E no event */
  504.     0xFF,    /* 8F no event */
  505.  
  506.     0xFF,     /* 90 no event */        
  507.     0xFF,    /* 91 no event */    
  508.     0xFF,    /* 92 no event */    
  509.     0xFF,    /* 93 no event */    
  510.     0xFF,    /* 94 no event */
  511.     0xFF,    /* 95 no event */
  512.     0xFF,    /* 96 no event */
  513.     0xFF,    /* 97 no event */
  514.     0xFF,    /* 98 no event */
  515.     0xFF,    /* 99 no event */
  516.     0xFF,    /* 9A no event */
  517.     0xFF,    /* 9B no event */
  518.     0xFF,    /* 9C no event */
  519.     0xFF,    /* 9D no event */
  520.     0xFF,    /* 9E no event */
  521.     0xFF,    /* 9F no event */
  522.  
  523.     0xFF,     /* A0 no event */        
  524.     0xFF,    /* A1 no event */    
  525.     0xFF,    /* A2 no event */    
  526.     0xFF,    /* A3 no event */    
  527.     0xFF,    /* A4 no event */
  528.     0xFF,    /* A5 no event */
  529.     0xFF,    /* A6 no event */
  530.     0xFF,    /* A7 no event */
  531.     0xFF,    /* A8 no event */
  532.     0xFF,    /* A9 no event */
  533.     0xFF,    /* AA no event */
  534.     0xFF,    /* AB no event */
  535.     0xFF,    /* AC no event */
  536.     0xFF,    /* AD no event */
  537.     0xFF,    /* AE no event */
  538.     0xFF,    /* AF no event */
  539.  
  540.     0xFF,     /* B0 no event */        
  541.     0xFF,    /* B1 no event */    
  542.     0xFF,    /* B2 no event */    
  543.     0xFF,    /* B3 no event */    
  544.     0xFF,    /* B4 no event */
  545.     0xFF,    /* B5 no event */
  546.     0xFF,    /* B6 no event */
  547.     0xFF,    /* B7 no event */
  548.     0xFF,    /* B8 no event */
  549.     0xFF,    /* B9 no event */
  550.     0xFF,    /* BA no event */
  551.     0xFF,    /* BB no event */
  552.     0xFF,    /* BC no event */
  553.     0xFF,    /* BD no event */
  554.     0xFF,    /* BE no event */
  555.     0xFF,    /* BF no event */
  556.  
  557.     0xFF,     /* C0 no event */        
  558.     0xFF,    /* C1 no event */    
  559.     0xFF,    /* C2 no event */    
  560.     0xFF,    /* C3 no event */    
  561.     0xFF,    /* C4 no event */
  562.     0xFF,    /* C5 no event */
  563.     0xFF,    /* C6 no event */
  564.     0xFF,    /* C7 no event */
  565.     0xFF,    /* C8 no event */
  566.     0xFF,    /* C9 no event */
  567.     0xFF,    /* CA no event */
  568.     0xFF,    /* CB no event */
  569.     0xFF,    /* CC no event */
  570.     0xFF,    /* CD no event */
  571.     0xFF,    /* CE no event */
  572.     0xFF,    /* CF no event */
  573.  
  574.     0xFF,     /* D0 no event */        
  575.     0xFF,    /* D1 no event */    
  576.     0xFF,    /* D2 no event */    
  577.     0xFF,    /* D3 no event */    
  578.     0xFF,    /* D4 no event */
  579.     0xFF,    /* D5 no event */
  580.     0xFF,    /* D6 no event */
  581.     0xFF,    /* D7 no event */
  582.     0xFF,    /* D8 no event */
  583.     0xFF,    /* D9 no event */
  584.     0xFF,    /* DA no event */
  585.     0xFF,    /* DB no event */
  586.     0xFF,    /* DC no event */
  587.     0xFF,    /* DD no event */
  588.     0xFF,    /* DE no event */
  589.     0xFF,    /* DF no event */
  590.  
  591.     0x3B,     /* E0 left control key */        
  592.     0x38,    /* E1 left shift key key */    
  593.     0x3A,    /* E2 left alt/option key */    
  594.     0x37,    /* E3 left GUI (windows/cmd) key */    
  595.     
  596.     0x3E,    /* E4 right control key */
  597.     0x3C,    /* E5 right shift key key */
  598.     0x3D,    /* E6 right alt/option key */
  599.     0x37,    /* E7 right GUI (windows/cmd) key */
  600.     0xFF,    /* E8 no event */
  601.     0xFF,    /* E9 no event */
  602.     0xFF,    /* EA no event */
  603.     0xFF,    /* EB no event */
  604.     0xFF,    /* EC no event */
  605.     0xFF,    /* ED no event */
  606.     0xFF,    /* EE no event */
  607.     0xFF,    /* EF no event */
  608.     
  609.     0xFF,     /* F0 no event */        
  610.     0xFF,    /* F1 no event */    
  611.     0xFF,    /* F2 no event */    
  612.     0xFF,    /* F3 no event */    
  613.     0xFF,    /* F4 no event */
  614.     0xFF,    /* F5 no event */
  615.     0xFF,    /* F6 no event */
  616.     0xFF,    /* F7 no event */
  617.     0xFF,    /* F8 no event */
  618.     0xFF,    /* F9 no event */
  619.     0xFF,    /* FA no event */
  620.     0xFF,    /* FB no event */
  621.     0xFF,    /* FC no event */
  622.     0xFF,    /* FD no event */
  623.     0xFF,    /* FE no event */
  624.     0xFF,    /* FF no event */
  625. };
  626.         
  627. static RoutineDescriptor gTimerCompletionUPP = BUILD_ROUTINE_DESCRIPTOR(uppTimerProcInfo, (ProcPtr)TimerCompletion);
  628.     
  629. void
  630. InitUSBKeyboard()
  631. {
  632.     handleKCHR = GetResource('KCHR',0);    // US keyboard mapping (handled differently by ADB Mgr)
  633.     HLock(handleKCHR);
  634.     KCHRptr = (UInt8 *)*handleKCHR;
  635. }
  636.  
  637.  
  638.  
  639. void PostUSBKeyToMac(UInt16 rawUSBkey)
  640. {
  641. static    UInt8    oldLEDState = 0x00;
  642. static    UInt8    newLEDState = 0x00;
  643.  
  644. static    UInt8    capsLockState = 0x00;
  645. static    UInt8    numLockState = 0x00;
  646. static    UInt8    scrollLockState = 0x00;
  647.  
  648. register UInt8    virtualKeycode, keystate;
  649.  
  650.     
  651.     if (KCHRptr == 0)
  652.     {
  653.         InitUSBKeyboard();
  654.     }
  655.     
  656.     keystate = (rawUSBkey & 0x8000) ? UP : DOWN;
  657.     rawUSBkey &= 0x0FF;
  658.     
  659.     if (keystate == DOWN)
  660.     {
  661.         newLEDState = oldLEDState;
  662.         
  663.         switch (rawUSBkey)
  664.         {
  665. // Note:  This switch statement is being left it to make it easy to add "toggling" keys in the future.
  666. //        it used to support toggled numlock & scroll lock...  It doesn't anymore.
  667.             case kUSBNumLockKey:
  668.                 newLEDState ^= (1 << kNumLockLED);
  669.                 keystate = (newLEDState & (1 << kNumLockLED)) ? DOWN : UP;
  670.                 break;
  671.         }
  672.         
  673.         if (newLEDState != oldLEDState)
  674.         {
  675.             oldLEDState = newLEDState;
  676.             //USBHIDControlDevice(kHIDSetLEDStateByBits, &newLEDState);
  677.         }
  678.     }
  679.     
  680.     if (rawUSBkey == kUSBNumLockKey)
  681.     {
  682.         return;
  683.     }
  684.     
  685.     // look up rawUSBkey in KMAP resource to get virtual keycode
  686.     if (rawUSBkey < sizeof(USBKMAPNumLockDown))
  687.     {
  688.         if (oldLEDState & kNumLockLEDMask)
  689.         {
  690.             virtualKeycode = USBKMAPNumLockDown[rawUSBkey];
  691.         }
  692.         else
  693.         {
  694.             virtualKeycode = USBKMAPNumLockUp[rawUSBkey];
  695.         }
  696.     } 
  697.     else 
  698.     {
  699. //        DebugStr("\pPostUSBKeyToMac: Need bigger KMAP table");
  700.         virtualKeycode = 0xFF;
  701.     }
  702.  
  703.     PostADBKeyToMac(virtualKeycode, keystate);
  704.     if (virtualKeycode == 0x7F)
  705.         PostADBKeyToMac(virtualKeycode, keystate);
  706. }
  707.  
  708. void 
  709. PostADBKeyToMac(UInt16 virtualKeycode, UInt8 state)
  710. {
  711.     UInt32    keyEventMsg;
  712.     EventKind keyEventKind;
  713.     
  714.     if (virtualKeycode > 127) return;  // not handled by MacOS!
  715.     
  716.     // stop repeating
  717.     LMSetKeyLast(0);
  718.     LMSetKbdVars(0);
  719.     
  720.     // update our keymap
  721.     SetBit((UInt8 *)myKeyMAP, virtualKeycode, state == DOWN ? 1 : 0);
  722.     LMSetKeyMap(&myKeyMAP);
  723.  
  724.     // set this keyboard as the last keyboard
  725.     LMSetKbdLast(FakeADBAddr);
  726.     LMSetKbdType(FakeKBDType);
  727.     
  728.     // call KeyTrans to get character code
  729.     virtualKeycode |= ((myKeyMAP[1]<<9) & 0x00FE00) | ((myKeyMAP[1]>>7) & 0x0100) | ((state==UP) ? 0x080 : 0);
  730.     keyEventMsg = KeyTranslate(KCHRptr, virtualKeycode , &keyTransState);
  731.     virtualKeycode &= 0x7F;
  732.     
  733.     if (gTMTaskData.theTask.qType < 0)    // task is active so kill it
  734.         RmvTime((QElemPtr)&gTMTaskData.theTask);
  735.  
  736.     if (keyEventMsg & 0xFFFF0000) {
  737.         // post event
  738.         UInt32 event = (keyEventMsg & 0xFF000000) | ((FakeADBAddr << 16) & 0x0FF0000) | ((virtualKeycode << 8) & 0x0FF00)  | ((keyEventMsg>>16) & 0x0FF);
  739.         if (state == DOWN) {
  740.             UInt32 ticks = TickCount();
  741.             LMSetKeyTime(ticks);
  742.             LMSetKeyRepTime(ticks);
  743.             LMSetKeyLast(event & 0x0FFFF);
  744.             LMSetKbdVars((event>>16) & 0x0FFFF);
  745.         }        
  746.         
  747.         keyEventKind = (state == DOWN ? keyDown : keyUp);
  748.         
  749.         if (/*!inMacsbug && */ (virtualKeycode == kRawEscapeKey)) {
  750.             gTMTaskData.eventNum = keyEventKind;
  751.             gTMTaskData.eventMsg = event;
  752.             
  753.             gTMTaskData.theTask.tmAddr = &gTimerCompletionUPP;
  754.             gTMTaskData.theTask.tmCount = 0;
  755.             gTMTaskData.theTask.tmWakeUp = 0;
  756.             gTMTaskData.theTask.tmReserved = 0;
  757.             InsTime((QElemPtr)&gTMTaskData.theTask);        // install our task
  758.             PrimeTime((QElemPtr)&gTMTaskData.theTask, 10L);    // run as soon as 68K interrupts are enabled
  759.         }
  760.         else {
  761.             // post the keys directly
  762.             MyPostEvent(keyEventKind, event);
  763.         }
  764.     }
  765.  
  766.     if (keyEventMsg & 0x0000FFFF) {
  767.         // post event
  768.         UInt32 event =((keyEventMsg<<16) & 0xFF000000) | ((FakeADBAddr << 16) & 0x0FF0000) | ((virtualKeycode << 8) & 0x0FF00)  | (keyEventMsg & 0x0FF);
  769.         if (state == DOWN){
  770.             UInt32 ticks = TickCount();
  771.             LMSetKeyTime(ticks);
  772.             LMSetKeyRepTime(ticks);
  773.             LMSetKeyLast(event & 0x0FFFF);
  774.             LMSetKbdVars((event>>16) & 0x0FFFF);
  775.         }        
  776.         
  777.         keyEventKind = (state == DOWN ? keyDown : keyUp);
  778.         
  779.         if (/*!inMacsbug && */ (virtualKeycode == kRawEscapeKey)) {
  780.             gTMTaskData.eventNum = keyEventKind;
  781.             gTMTaskData.eventMsg = event;
  782.             
  783.             gTMTaskData.theTask.tmAddr = &gTimerCompletionUPP;
  784.             gTMTaskData.theTask.tmCount = 0;
  785.             gTMTaskData.theTask.tmWakeUp = 0;
  786.             gTMTaskData.theTask.tmReserved = 0;
  787.             InsTime((QElemPtr)&gTMTaskData.theTask);        // install our task
  788.             PrimeTime((QElemPtr)&gTMTaskData.theTask, 10L);    // run as soon as 68K interrupts are enabled
  789.         }
  790.         else {
  791.             // post the keys directly
  792.             MyPostEvent(keyEventKind, event);
  793.         }
  794.     }
  795. }
  796.  
  797. // Sets the bitmapArray[index] to value
  798. // returns old value;
  799. Boolean
  800. SetBit(UInt8 *bitmapArray, UInt16 index, Boolean value)
  801. {    
  802.     UInt32    mask = 0x1 << (index % 8 );
  803.     Boolean    oldVal;
  804.     
  805.     oldVal = (bitmapArray[index/8] & mask) ? TRUE : FALSE;
  806.     
  807.     if (value){
  808.         bitmapArray[index/8] |= mask;
  809.     }else{
  810.         bitmapArray[index/8] &= ~mask;
  811.     }
  812.     
  813.     return (oldVal);
  814. }
  815.  
  816. void USBDemoKeyIn(UInt32 refcon, void * theData)
  817. {
  818. #pragma unused (refcon)
  819.  
  820. USBHIDDataPtr    pTheKeyboardData;
  821. register        UInt8    i;
  822.  
  823.     pTheKeyboardData = (USBHIDDataPtr)theData;
  824.     for (i=0; i<pTheKeyboardData->kbd.keycount; i++)
  825.     {
  826.         // no shim installed, let's just post some Macintosh keyevents
  827.         PostUSBKeyToMac(pTheKeyboardData->kbd.usbkeycode[i]);
  828.     }
  829.  
  830. }
  831.  
  832. OSStatus USBHIDControlDevice(UInt32 theControlSelector, void * theControlData)
  833. {
  834. #pragma unused (theControlData)
  835.  
  836.     switch (theControlSelector)
  837.     {
  838.         case kHIDSetLEDStateByBits:
  839.             break;
  840.             
  841.         case kHIDEnableDemoMode:
  842.             USBExpertStatus(myParamBlock.deviceRef, "\pTenKey Driver: Demo Mode Enabled", myParamBlock.deviceRef);
  843.             break;
  844.  
  845.         case kHIDDisableDemoMode:
  846.             USBExpertStatus(myParamBlock.deviceRef, "\pTenKey Driver: Demo Mode Disabled", myParamBlock.deviceRef);
  847.             break;
  848.  
  849.         default:
  850.             return paramErr;
  851.     }
  852.     return 0;
  853. }
  854.  
  855. void ProcessInterruptReport(UInt8 hidReport[])
  856. {
  857.  
  858. UInt8    i, j, newkey, oldkey, deltas;
  859.  
  860. UInt8        changedmodifiers, keycount;
  861. USBHIDData    theKeyboardData;
  862. Boolean        keypressedflag, keyreleasedflag;
  863.     
  864.     deltas = 0;
  865.     
  866.     if (myParamBlock.hidEmulationInit == false)
  867.     {
  868.         myParamBlock.hidEmulationInit = true;
  869.         for (i = 0; i < kKeyboardReportSize; i++)
  870.             myParamBlock.oldHIDReport[i] = 0;
  871.     };
  872.     
  873.     myParamBlock.oldHIDReport[1] = 0x0;
  874.     hidReport[1] = 0x0;
  875.     
  876.     for (i = 0; i < kKeyboardReportSize; i++)
  877.     {
  878.         if (hidReport[i] != myParamBlock.oldHIDReport[i])
  879.         {
  880.             deltas++;
  881.         }
  882.     }
  883.     
  884.     if ((myParamBlock.sendRawReportFlag) && deltas)
  885.     {
  886.         USBDemoKeyIn(0xff, (void *)&hidReport[0]);
  887.     }
  888.     else
  889.     {
  890.         if (deltas)
  891.         {
  892.             keycount = 0;
  893.             changedmodifiers = hidReport[0] ^  myParamBlock.oldHIDReport[0];
  894.             
  895.             if (changedmodifiers)
  896.             {
  897.                 for (i = 0; i < kKeyboardModifierBits; i++)
  898.                 {
  899.                     if (changedmodifiers & (1 << i))
  900.                     {
  901.                         if (hidReport[0] & (1 << i))
  902.                         {
  903.                             theKeyboardData.kbd.usbkeycode[keycount++] = (0xe0 + i);
  904.                         }
  905.                         else
  906.                         {
  907.                             theKeyboardData.kbd.usbkeycode[keycount++] = (0x80e0 + i);
  908.                         }
  909.                     }
  910.                 }
  911.                 myParamBlock.oldHIDReport[0] = hidReport[0];
  912.             }
  913.             
  914.             if ((hidReport[kKeyboardOffsetToKeys] == 0) || (hidReport[kKeyboardOffsetToKeys] > 0x03))
  915.             {
  916.                 // While this double loop may look strange, I refer you to appendix C, of the HID Devices specification.
  917.                 // pp. 73 & 74 clearly state that report order is abitrary and does not reflect the order of events.
  918.                 // to quote: "The order of keycodes in array fields has no significance.  Order determination is done
  919.                 // by the host software comparing the contents of the previous report to the current report.  If two or 
  920.                 // more keys are pressed in one report, their order is indeterminate.  Keyboards may buffer events that
  921.                 // would have otherwise resulted in multiple events in a single report".
  922.                 
  923.                 // Because this specification (or lack thereof) states that the order is indeterminant, we have to 
  924.                 // check all the old keys against all the new keys...
  925.                 
  926.                 for (i = kKeyboardOffsetToKeys; i < (kKeyboardOffsetToKeys + kKeyboardReportKeys); i++)
  927.                 {
  928.                     keypressedflag = true;
  929.                     newkey = hidReport[i];
  930.                     
  931.                     keyreleasedflag = true;
  932.                     oldkey = myParamBlock.oldHIDReport[i];                            
  933.                     for (j = kKeyboardOffsetToKeys; j < (kKeyboardOffsetToKeys + kKeyboardReportKeys); j++)                                // then look through all the keys that were previously and are currently reported as pressed
  934.                     {                                    
  935.                         if (newkey == myParamBlock.oldHIDReport[j])        // was this new key already pressed?
  936.                         {                                
  937.                             keypressedflag = false;                        // if it was, then don't report it as being pressed.
  938.                         }
  939.                         
  940.                         if (oldkey == hidReport[j])                        // Is the old key still pressed?
  941.                         {                                
  942.                             keyreleasedflag = false;                    // If yes, then don't report it as being released
  943.                         }
  944.                     }
  945.                     if ((newkey > 0x03) && keypressedflag)
  946.                     {
  947.                         theKeyboardData.kbd.usbkeycode[keycount++] = newkey;
  948.                     }
  949.                     
  950.                     if ((oldkey > 0x03) && keyreleasedflag)
  951.                     {
  952.                         theKeyboardData.kbd.usbkeycode[keycount++] = (oldkey + 0x8000);
  953.                     }
  954.                     
  955.                     if (keycount > 20)                                    // a worse case scenario is:
  956.                     {                                                    // 4 modifier keys up
  957.                         break;                                            // the other 4 modifier keys down
  958.                     }                                                    // 6 previously reported keys up
  959.                 }                                                        // 6 newly reported keys down
  960.                                                                         // 6 + 6 + 4 + 4 = 20
  961.                 for (i = 0; i<kKeyboardReportSize; i++)
  962.                 {
  963.                     myParamBlock.oldHIDReport[i] = hidReport[i];
  964.                 };
  965.             }
  966.             
  967.             if (keycount)
  968.             {
  969.                 theKeyboardData.kbd.keycount = keycount;
  970.                 USBDemoKeyIn(myParamBlock.interruptRefcon, (void *)&theKeyboardData);
  971.             }
  972.         }
  973.     }
  974. }
  975.  
  976. // This version of PostEvent works around a problem in the force-quit detection where the EventKind
  977. // is being treated (incorrectly) as an UInt32  
  978. pascal OSErr MyPostEvent(EventKind eventNum, UInt32 eventMsg)
  979. {
  980.     return (OSErr) CallUniversalProc(
  981.                  (UniversalProcPtr)GetOSTrapAddress(0xA02F),
  982.                  kRegisterBased |
  983.                  RESULT_SIZE(SIZE_CODE(sizeof(OSErr))) |
  984.                  REGISTER_ROUTINE_PARAMETER(1, kRegisterA0, SIZE_CODE(sizeof(UInt32))) |
  985.                  REGISTER_ROUTINE_PARAMETER(2, kRegisterD0, SIZE_CODE(sizeof(UInt32))),
  986.                   eventNum, eventMsg); // parameter(s)
  987. }
  988.  
  989. void TimerCompletion(TMTaskData* taskData)
  990. {
  991.     OSErr err;
  992.     
  993.     RmvTime((QElemPtr)&taskData->theTask);
  994.  
  995.     err = MyPostEvent(taskData->eventNum, taskData->eventMsg);
  996. }
  997.